<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>显示所有康复计划</title>
	<link rel="stylesheet" href="css/bootstrap.css">
	<script src="js/jquery.js" type="text/javascript"></script>
	<script src="js/bootstrap.js" type="text/javascript"></script>
	<script src="js/jsrender.js" type="text/javascript"></script>
	<script src="js/common.js" type="text/javascript"></script>
<script type="text/javascript">
	$(function() {
        function showAll(){
            //请求所有部门信息
			// alert(localStorage.getItem("position"))
			// alert(localStorage.getItem("account"))
			if(localStorage.getItem("position")==1) {
				$.ajax({
					url: GC.bctx + "recoveryRegister/list",//后端请求映射路径
					type: "GET",
					headers: {"Authorization": localStorage.getItem("token")},
					success: function (r) {
						if (r.successed) {
							//jsrender模板渲染
							$("#recoveryRegisters").html($("#recoveryRegistersTemplate").render(r.data))
						} else {
							window.location.href = "login.html"
						}
					}
				})
			}
			else{
				$("#nurseIDLabel").prop("class","d-none")
				$("#nurseNameLabel").prop("class","d-none")
				$("#operation").prop("class","d-none")
				$("#newRegister").prop("class","d-none")
				$.ajax({
					url: GC.bctx + "recoveryRegister/nurseList/"+localStorage.getItem("account"),//后端请求映射路径
					type: "GET",
					headers: {"Authorization": localStorage.getItem("token")},
					success: function (r) {
						if (r.successed) {
							//jsrender模板渲染
							$("#recoveryRegisters").html($("#recoveryRegistersNurseTemplate").render(r.data))
						} else {
							window.location.href = "login.html"
						}
					}
				})
			}
        }
        showAll()

        $("form").submit(function(){
        	if($("#recoveryRegisterID").val()==""&&$("#nurseID").val()==""&&$("#nurseName").val()==""&&$("#customerID").val()==""&&$("#customerName").val()==""){
				// alert("查询全为空")
        		showAll()
				return false;
			}
			if($("#nurseID").val()!=""&&isNaN($("#nurseID").val())){
				alert("护士编号必须为纯数字!")
				return false;
			}
			if($("#customerID").val()!=""&&isNaN($("#customerID").val())){
				alert("客户编号必须为纯数字!")
				return false;
			}
        	if (localStorage.getItem("position")==1) {
				$.ajax({
					url: GC.bctx + "recoveryRegister/find",
					type: "GET",
					data: $("form").serialize(),//需要配上属性name：name="id" name="type"
					headers: {"Authorization": localStorage.getItem("token")},
					success: function (r) {
						if (r.successed) {
							//成功了渲染
							$("#recoveryRegisters").html($("#recoveryRegistersTemplate").render(r.data))
						} else {
							window.location.href = "login.html"
						}
					}
				})
			}
        	else{
				$.ajax({
					url: GC.bctx + "recoveryRegister/nurseFind/"+localStorage.getItem("account"),
					type: "GET",
					data: $("form").serialize(),//需要配上属性name：name="id" name="type"
					headers: {"Authorization": localStorage.getItem("token")},
					success: function (r) {
						if (r.successed) {
							//成功了渲染
							$("#recoveryRegisters").html($("#recoveryRegistersNurseTemplate").render(r.data))
						} else {
							window.location.href = "login.html"
						}
					}
				})
			}
            //阻止表单正常提交
            return false;
        })

        $("#newRegister").click(function() {
            window.location.href="insertRecoveryRegister.html"
        })


        $("#recoveryRegisters").on("click",".btn-delete",function () {
            //var id=$(this).parent().parent().find("td:first").text()   //传统方法
            var id = $(this).attr("data-id")            //取值方法
            //删除2：二次确认
            if(!window.confirm("确定删除以下编号的康复登记吗？ \n" + id)){
                return;
            }
            if(!window.confirm("删除后数据无法恢复，再次确认")){
                return;
            }
            //ajax提交请求
            $.ajax({
                url:GC.bctx + "recoveryRegister/delete/"+id,
                type:"DELETE",
                headers: {"Authorization": localStorage.getItem("token")},
                success:function(r){
                    if(r.successed){
                        //成功后
                        alert(r.message)
                        showAll()
                    }else{
                        alert(r.message)
                    }
                }
            })
        })



	})
</script>
</head>
<body>
<form  action="find" method="get" class="container-fluid text-center">
	<label>康复登记编号: <input id="recoveryRegisterID" name="recoveryRegisterID" type="text"></label>
	<label name="nurseIDLabel" id="nurseIDLabel">护士编号: <input id="nurseID" name="nurseID" type="text"></label>
	<label name="nurseNameLabel" id="nurseNameLabel">护士名: <input id="nurseName" name="nurseName"  type="text"></label>
	<label>客户编号: <input id="customerID" name="customerID"  type="text"></label>
	<label>客户名: <input id="customerName" name="customerName"  type="text"></label>
	<input type="submit" value="查 询">
	<input type="reset" value="重 置">
</form>
<button id="newRegister" href="#" class="btn btn-primary">新增康复登记</button>

<script id="recoveryRegistersTemplate" type="text/x-jsrender">
	<tr align="center">
		<td>{{:recoveryRegisterID}}</td>
		<td>{{:nurseID}}</td>
		<td>{{:nurseName}}</td>
		<td>{{:customerID}}</td>
		<td>{{:customerName}}</td>
		<td>{{:recoveryRegisterPlan}}</td>
		<td colspan="2" align="center">
            <a href="updateRecoveryRegister.html?id={{:recoveryRegisterID}}" class="btn btn-update btn-info" data-id="{{:recoveryRegisterID}}">修改</a>
            <button name="deleteRegister" class="btn btn-delete btn-danger" data-id="{{:recoveryRegisterID}}">删除</button>
         </td>
	</tr>
    </script>

<script id="recoveryRegistersNurseTemplate" type="text/x-jsrender">
	<tr align="center">
		<td>{{:recoveryRegisterID}}</td>
		<td>{{:nurseID}}</td>
		<td>{{:nurseName}}</td>
		<td>{{:customerID}}</td>
		<td>{{:customerName}}</td>
		<td>{{:recoveryRegisterPlan}}</td>
		<td class="d-none" colspan="2" align="center">
            <a href="updateRecoveryRegister.html?id={{:recoveryRegisterID}}" class="btn btn-update btn-info" data-id="{{:recoveryRegisterID}}">修改</a>
            <button name="deleteRegister" class="btn btn-delete btn-danger" data-id="{{:recoveryRegisterID}}">删除</button>
         </td>
	</tr>
    </script>

<table class="table table-bordered table-hover">
	<tr class="text-center">
		<th>康复登记id</th>
		<th>护士id</th>
		<th>护士名</th>
		<th>客户id</th>
		<th>客户名</th>
		<th>康复计划</th>
		<th id="operation">操作</th>
	</tr>
	<tbody id="recoveryRegisters"></tbody>
</table>

</body>
</html>